package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author syh
 * @create 2023-08-19 9:55
 */
@Tag(name = "菜单功能模块")
@RestController
@RequestMapping(value="/admin/system/sysMenu")
public class SysMenuController {
    @Resource
    SysMenuService sysMenuService;

    @Operation(summary = "查询可用的菜单列表并组装为父子集合和角色已分配的菜单id集合")
    @GetMapping("role/listMenus/{roleId}")
    public Result<Map<String,Object>> listRoleMenus(@PathVariable("roleId")Long roleId){
        Map<String,Object> map=sysMenuService.listRoleMenus(roleId);
        return Result.build(map,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除指定id的菜单")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable("id")Long id){
        sysMenuService.deleteById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据id查询菜单")
    @GetMapping("{id}")
    public Result<SysMenu> getById(@PathVariable("id")Long id){
        SysMenu sysMenu=sysMenuService.getById(id);
        return Result.build(sysMenu,ResultCodeEnum.SUCCESS);
    }
    @Operation(summary = "根据id更新菜单")
    @PutMapping()
    public Result<Objects> updateById(@RequestBody SysMenu sysMenu){
       sysMenuService.updateById(sysMenu);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @Operation(summary = "新增菜单")
    @PostMapping()
    public Result<Objects> save(@RequestBody SysMenu sysMenu){
        sysMenuService.save(sysMenu);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }





    @Operation(summary = "查询所有菜单")
    @GetMapping("/findNodes")
    public Result<List<SysMenu>> findNodes() {
        List<SysMenu> list = sysMenuService.findNodes();
        return Result.build(list , ResultCodeEnum.SUCCESS) ;
    }
}
